//
//  KATTableCell.m
//  KATFramework
//
//  Created by Kantice on 15/8/5.
//  Copyright (c) 2015年 KatApp. All rights reserved.
//

#import "KATTableCell.h"
#import "KATTableView.h"



@implementation KATTableCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */


+ (instancetype)cell
{
    return [[[self alloc] init] autorelease];
}


//初始化
- (instancetype)init
{
    self=[super init];
    
    self.tableView=nil;
    self.type=0;
    self.num=0;
    self.data=0;
    self.index=0;
    self.value=0.0;
    self.movingTime=0.001f;
    self.state=TABLE_CELL_STATE_READY;
    self.isHighSpeed=NO;
    self.isTapedColor=NO;
    self.message=nil;
    self.data=nil;
    self.image=nil;
    self.image2=nil;
    self.image3=nil;
    self.title=nil;
    self.content=nil;
    self.label1=nil;
    self.label2=nil;
    self.label3=nil;
    self.label4=nil;
    self.label5=nil;
    self.richTitle=nil;
    self.richContent=nil;
    self.richLabel=nil;
    self.text=nil;
    self.badge=nil;
    self.backLabel=nil;
    self.backLayer=nil;
    self.midLayer=nil;
    self.faceLayer=nil;
    self.button1=nil;
    self.button2=nil;
    self.button3=nil;
    self.spButton=nil;
    self.backButton1=nil;
    self.backButton2=nil;
    self.backButton3=nil;
    self.backButton4=nil;
    self.backButton5=nil;
    self.backButton6=nil;
    self.backButton7=nil;
    self.line1=nil;
    self.line2=nil;
    
    return self;
}


//左移
- (void)moveLeftWithDistance:(double)distance andAnimated:(BOOL)animated
{
    if(_state!=TABLE_CELL_STATE_LEFT)
    {
        if(_midLayer && _backLayer && distance>0)
        {
            if(animated)
            {
                [UIView animateWithDuration:_movingTime*fabs(-distance-_midLayer.frame.origin.x) animations:^
                {
                    _midLayer.frame=CGRectMake(-distance, 0, _midLayer.frame.size.width, _midLayer.frame.size.height);
                }
                completion:^(BOOL finished)
                {
                    _data.state=TABLE_CELL_STATE_LEFT;
                    _state=TABLE_CELL_STATE_LEFT;
                    
                    if(_tableView && _tableView.eventDelegate && [_tableView.eventDelegate respondsToSelector:@selector(tableView:didMoveCellToLeft:)])
                    {
                        [_tableView.eventDelegate tableView:_tableView didMoveCellToLeft:self];
                    }
                }];
            }
            else
            {
                _midLayer.frame=CGRectMake(-distance, 0, _midLayer.frame.size.width, _midLayer.frame.size.height);
                
                _data.state=TABLE_CELL_STATE_LEFT;
                _state=TABLE_CELL_STATE_LEFT;
                
                if(_tableView && _tableView.eventDelegate && [_tableView.eventDelegate respondsToSelector:@selector(tableView:didMoveCellToLeft:)])
                {
                    [_tableView.eventDelegate tableView:_tableView didMoveCellToLeft:self];
                }
            }
        }
    }
}

//右移
- (void)moveRightWithDistance:(double)distance andAnimated:(BOOL)animated
{
    if(_state!=TABLE_CELL_STATE_RIGHT)
    {
        if(_midLayer && _backLayer && distance>0)
        {
            if(animated)
            {
                [UIView animateWithDuration:_movingTime*fabs(distance-_midLayer.frame.origin.x) animations:^
                {
                    _midLayer.frame=CGRectMake(distance, 0, _midLayer.frame.size.width, _midLayer.frame.size.height);
                }
                completion:^(BOOL finished)
                {
                    _data.state=TABLE_CELL_STATE_RIGHT;
                    _state=TABLE_CELL_STATE_RIGHT;
                    
                    if(_tableView && _tableView.eventDelegate && [_tableView.eventDelegate respondsToSelector:@selector(tableView:didMoveCellToRight:)])
                    {
                        [_tableView.eventDelegate tableView:_tableView didMoveCellToRight:self];
                    }
                }];
            }
            else
            {
                _midLayer.frame=CGRectMake(distance, 0, _midLayer.frame.size.width, _midLayer.frame.size.height);
                
                _data.state=TABLE_CELL_STATE_RIGHT;
                _state=TABLE_CELL_STATE_RIGHT;
                
                if(_tableView && _tableView.eventDelegate && [_tableView.eventDelegate respondsToSelector:@selector(tableView:didMoveCellToRight:)])
                {
                    [_tableView.eventDelegate tableView:_tableView didMoveCellToRight:self];
                }
            }
        }
    }
}


//取消移动，回到原位
- (void)backToPositionWithAnimated:(BOOL)animated
{
    if(_state!=TABLE_CELL_STATE_READY)
    {
        if(_midLayer && _backLayer)
        {
            if(animated && _midLayer.frame.origin.x!=0)
            {                
                [UIView animateWithDuration:_movingTime*fabs(_midLayer.frame.origin.x) animations:^
                {
                     _midLayer.frame=CGRectMake(0, 0, _midLayer.frame.size.width, _midLayer.frame.size.height);
                }
                completion:^(BOOL finished)
                {
                    _data.state=TABLE_CELL_STATE_READY;
                    _state=TABLE_CELL_STATE_READY;
                    
                    if(_tableView && _tableView.eventDelegate && [_tableView.eventDelegate respondsToSelector:@selector(tableView:didMoveCellBack:)])
                    {
                        [_tableView.eventDelegate tableView:_tableView didMoveCellBack:self];
                    }
                }];
            }
            else
            {
                _midLayer.frame=CGRectMake(0, 0, _midLayer.frame.size.width, _midLayer.frame.size.height);
                
                _data.state=TABLE_CELL_STATE_READY;
                _state=TABLE_CELL_STATE_READY;
                
                if(_tableView && _tableView.eventDelegate && [_tableView.eventDelegate respondsToSelector:@selector(tableView:didMoveCellBack:)])
                {
                    [_tableView.eventDelegate tableView:_tableView didMoveCellBack:self];
                }
            }
        }
    }
}


//描述
- (NSString *)description
{
    return [NSString stringWithFormat:@"Cell:%i Title:%@ Y:%.1lf!",_num,_data.title,self.frame.origin.y];
}


- (void)dealloc
{
//    NSLog(@"KATTableCell:%i is dealloc!",_num);
    
    [_message release];
    [_data release];
    [_image release];
    [_image2 release];
    [_image3 release];
    [_title release];
    [_content release];
    [_label1 release];
    [_label2 release];
    [_label3 release];
    [_label4 release];
    [_label5 release];
    [_richTitle release];
    [_richContent release];
    [_richLabel release];
    [_text release];
    [_badge release];
    [_backLabel release];
    [_backLayer release];
    [_midLayer release];
    [_faceLayer release];
    [_button1 release];
    [_button2 release];
    [_button3 release];
    [_spButton release];
    [_backButton1 release];
    [_backButton2 release];
    [_backButton3 release];
    [_backButton4 release];
    [_backButton5 release];
    [_backButton6 release];
    [_backButton7 release];
    [_line1 release];
    [_line2 release];
    
    [super dealloc];
}


@end
